<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 *
 * 关键词
 * @author 66diqiu-hdk
 *
 */
class Admin_Words_module extends CI_Module {
	/**
	 *
	 * 构造函数
	 */
	public function __construct(){
		parent::__construct();
		$this->load->model('admin.Words_model');
		$this->load->model('admin.Adminuser_model');
	}

	/**
	 *
	 * 关键词
	 */
	function index(){
		$this->Adminuser_model->is_login();
		

		$result = $this->Words_model->get_list();
		$data['list'] = $result;
		$data['add_url'] = site_url('admin/words/add');   
        $data['index_url'] = site_url('admin/words/index');
        $data['get_url'] = site_url('admin/get_links/add');
		$this->load->view('words/index',$data);
	}
	/**
	 *
	 * 关键词的修改
	 */
	function add(){
		$data['form_post'] = site_url('admin/words/save');
		$data['check_name'] = site_url("admin/words/check_name?id=0");
        $data['add_url'] = site_url('admin/words/add');   
        $data['index_url'] = site_url('admin/words/index');
		$this->load->view('words/add',$data);
	}
	/**
	 *
	 * 关键词的修改
	 */
	function edit(){
		$id = $this->input->get('id');
		$row = $this->Words_model->get_row(array('id'=>$id));
		$data['row'] = $row;
		//数据保存URL
		$data['form_post'] = site_url('admin/words/save');
		$data['check_name'] = site_url("admin/words/check_name?id=$id");
		$data['index_url'] = site_url('admin/words/index');
		$this->load->view('words/edit',$data);
	}
	/**
	 *
	 * 删除操作
	 */
	function remove(){
		$id = $this->input->get('id');
		if(empty($id)){
			$post_id = $this->input->post('id');
			$res = $this->Words_model->remove_row("id in ($post_id)");
		}else{
			$res = $this->Words_model->remove_row(array('id'=>$id));
		}
		if($res){
			$this->success('操作成功',site_url('admin/words/index'),true);
		}else{
			$this->error('操作失败','',true);
		}
	}

	/**
	 * 提交保存关键词
	 */
	function save(){
		$data = $this->input->post('Form');
		$data['sid'] = $GLOBALS['sid'];
		$str = explode(",", str_replace("，", ",", $data['words']));
		$data['words'] = implode(",", array_unique($str));
		$id = $this->input->post('id');
		if(empty($id)){
			//操作日志
			$log = array('action' => 'add');
			$data['create_person'] = $this->session->userdata('admin_user_name');
			$data['update_person'] = $this->session->userdata('admin_user_name');
			$data['create_date'] = date('Y-m-d H:i:s',now());
			$data['update_date'] = date('Y-m-d H:i:s',now());
			$res = $this->Words_model->add_row($data);
		}else{
			//操作日志
			//			$log = array('action' => 'edit');
			$data['update_person'] = $this->session->userdata('admin_user_name');
			$data['update_date'] = date('Y-m-d H:i:s',now());
			$res = $this->Words_model->edit_row($data,array('id'=>$id));
		}
		if($res){

			//操作日志添加
			$log['log'] = '关键词('.$data['name'].')';
			//			$this->_record_log($log);

			$this->success('操作成功',site_url('admin/words/index'),true);
		}else{
			$this->error('操作失败',site_url('admin/words/index'),true);
		}
	}
	/**
	 *
	 * 判断名称是否存在
	 */
	function check_name(){
		$arg_get = $this->input->get();
		$row = $this->Words_model->get_row(array('name'=>$arg_get['Form']['name'],'id !='=>$arg_get['id']));
		if(!empty($row) && is_array($row)){
			$this->ajaxReturn('词库名称已存在!');
		}else{
			$this->ajaxReturn(true);
		}
	}


}